<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/5 0005
 * Time: 09:26
 */

namespace app\admin\controller;


use app\common\service\UploadService;
use app\common\validate\IDMustBePostiveInt;
use app\common\validate\GoodsValidate;
use think\Exception;
use think\facade\Request;
use app\common\model\Goods as GoodsModel;
use app\common\model\GoodsImages as GoodsImagesModel;
use app\common\model\Category as CategoryModel;
use app\common\model\Brand as BrandModel;

class Goods extends Common
{

    /**
     * 商品列表
     * @return \think\response\Json|\think\response\View
     * @throws \think\exception\DbException
     */
    public static function index() {
        if (Request::isAjax()) {
            $where = [];
            if (input('param.department_id', 0)) {
                $where['department_id'] = input('param.department_id');
            }
            $keyword = input('param.keyword', '');
            $data = (GoodsModel::getList($where, $keyword))->toArray();
            return return_data('商品列表', 0, $data['data'], $data['total']);
        }
        return view();
    }

    /**
     * 保存商品
     * @return GoodsModel|array|null|\think\response\Json|\think\response\View
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function save_goods() {
        $id = input('param.id', 0);
        if (request()->isAjax()) {

            $goods_validate = new GoodsValidate();
            $goods_validate->goCheck();
            $param = $goods_validate->getDataByRule(input('param.'));

            $data = GoodsModel::createOrUpdateGoods($param, $id);
            return $data;
        } else {
            $data = [];
            if ($id > 0) {
                $data = GoodsModel::get($id);
            }

            $this->assign('data', $data);

            // 分类
            $category = CategoryModel::get_category();
            $this->assign('category', $category);

            // 品牌
            $brand = BrandModel::all();
            $this->assign('brand', $brand);

            return view();
        }
    }

    /**
     * 修改商品排序
     * @return string|\think\response\Json
     * @throws Exception
     */
    public function save_goods_sort() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        if ($this->request->isAjax()) {
            $sort = input('param.sort');
            $data = GoodsModel::saveSort($id, $sort);
            return $data;
        }
        return 'fail';
    }


    /**
     * 删除goods
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function delete_goods() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        $goods = GoodsModel::get($id);
        if (!$goods) {
            return return_data('商品不存在');
        }
        try {
            $goods->delete();
            return return_data('删除成功！');
        } catch (Exception $e) {
            return return_data($e->getMessage(), 1);
        }
    }

    /**
     * 设置商品图片
     * @return array|\think\response\View
     * @throws Exception
     */
    public function set_image() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        if ($this->request->isAjax()) {
            $type = input('param.type'); // 图片类型   1=》相册图片（第一张为封面图）   2=》详情图片
            $data = UploadService::upload_image();
            if ($data['code'] == 0) {
                // 上传成功，添加商品图片数据
                GoodsImagesModel::createGoodsImage($id, $type, $data['path']);
            }
            return $data;
        }

        // 获取已有的图片
        $photo_album_images = GoodsImagesModel::getList($id, 1);
        $content_images = GoodsImagesModel::getList($id, 2);
        return view('', [
            'photo_album_images' => $photo_album_images,
            'content_images' => $content_images
        ]);
    }

    /**
     * 修改图片排序
     * @return string|\think\response\Json
     * @throws Exception
     */
    public function save_img_sort() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        if ($this->request->isAjax()) {
            $sort = input('param.sort');
            $data = GoodsImagesModel::saveImgSort($id, $sort);
            return $data;
        }
        return 'fail';
    }

    /**
     * 删除图片
     * @return \think\response\Json
     * @throws Exception
     * @throws Exception\DbException
     */
    public function delete_image() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        $image = GoodsImagesModel::get($id);
        if (!$image) {
            return_data('图片不存在', 1);
        }
        $image->delete();
        return return_data('删除成功！');
    }
}